home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / MPW IIGS SC / SC.024.Teach / teach.p / uWindow.inc.p < prev    next >
Encoding:
Text File  |  1990-06-24  |  11.9 KB  |  483 lines  |  [TEXT/MPS ]

  1. {**********************************************************************
  2. {*
  3. {* Teach uWindow.inc.p -- Version 3.0  (implementation)
  4. {*
  5. {* Copyright (c)
  6. {* Apple Computer, Inc.  1986-1990
  7. {* All Rights Reserved.
  8. {*
  9. {* Developer Technical Support Apple II Sample Code
  10. {*
  11. {* This file contains the code which implements  
  12. {* windows in the Teach program.
  13. {*
  14. {**********************************************************************}
  15. {$R-}
  16.  
  17.  
  18.  
  19. PROCEDURE Debug;  INLINE $0000;
  20.  
  21.  
  22. {***********************************************************************
  23. *
  24. * smartGetWTitle
  25. *
  26. * This function returns a pointer to the window title pascal string.
  27. * This is a little more complicated than it used to be, given that
  28. * NewWindow2 clones the string for the window into an unlocked handle.
  29. * GetWTitle returns a handle with the hi-bit set (to distinguish it
  30. * from a reguler pointer).  If the hi-bit is set, then we want to
  31. * dereference the handle to a pointer, and return that instead.
  32. *
  33. ***********************************************************************}
  34. function smartGetWTitle(wptr : GrafPortPtr): StringPtr;
  35.  
  36. var    wtitle : Ptr;
  37.  
  38. begin
  39.     wtitle := GetWTitle(wptr);
  40.     if (BAND4(longint(wtitle),$80000000) <> 0) 
  41.         then wtitle := Handle(BAND4(longint(wtitle), $7FFFFFFF))^;
  42.     smartGetWTitle := StringPtr(wtitle);
  43. end;
  44.  
  45. {**********************************************************************}
  46. {
  47. { drawThisWindow
  48. {
  49. { This routine draws the contents of all the windows.
  50. {
  51. {**********************************************************************}
  52. procedure drawThisWindow;
  53.     begin
  54.         DrawControls(GetPort);
  55.     END;
  56.     
  57. {**********************************************************************}
  58. {
  59. { doCloseTop
  60. {
  61. { This routine closes the topmost window.  We do a little work to
  62. { prevent the main window from being closed.
  63. {
  64. {**********************************************************************}
  65. procedure doCloseTop;
  66.     var
  67.         refCon    : Longint;
  68.         
  69.     begin
  70.         refCon := GetWRefCon(FrontWindow);
  71.         { Get rid of the path name handle that we keep in the refcon. }
  72.         { Note that it is zero if the window is untitled }
  73.         if refCon <> 0 then DisposeHandle(handle(refCon));
  74.  
  75.         { Close the window.  (This kills the window title handle.) }
  76.         CloseWindow(FrontWindow);
  77.     end;
  78.  
  79. {**********************************************************************}
  80. {
  81. { putFileIntoWindow
  82. {
  83. { This routine opens the specified file, reads its contents in and
  84. { sets the text of the text edit control of the top window to that text
  85. {
  86. {**********************************************************************}
  87. procedure    putFileIntoWindow (c1Hndl : handle);
  88.     var
  89.         openBlock : OpenRecGS;
  90.         closeBlock : RefNumRecGS;
  91.         readBlock : IORecGS;
  92.         theHndl : handle;
  93.         temp : integer;
  94.         
  95.     begin
  96.         with openBlock do begin
  97.             pCount := 15;
  98.             pathname := GSString255Ptr(c1Hndl^);
  99.             requestAccess := 0;
  100.             resourceNumber := 0;
  101.             optionList := NIL;
  102.         end;
  103.         
  104.         OpenGS (OpenBlock);
  105.         if _toolErr <> 0 then 
  106.             begin
  107.                 temp := ErrorWindow(0,c1Hndl^,_ToolErr);
  108.                 exit(putfileIntoWindow);
  109.             end;
  110.  
  111.         with CloseBlock do begin
  112.             pCount := 1;
  113.             refNum := OpenBlock.refNum;
  114.         end;
  115.  
  116.         theHndl := NewHandle (openBlock.eof,userID,0,NIL);
  117.         if _toolErr <> 0 then 
  118.             begin
  119.                 temp := ErrorWindow(0,c1Hndl^,_ToolErr);
  120.                 CloseGS(closeBlock);
  121.                 exit(putfileIntoWindow);
  122.             end;
  123.             
  124.         HLock (theHndl);
  125.  
  126.         with readBlock do begin
  127.             pCount := 4;
  128.             refNum := openBlock.refNum;
  129.             DataBuffer := theHndl^;
  130.             requestCount := openBlock.eof;
  131.         end;
  132.         ReadGS (readBlock);
  133.         if _toolErr <> 0 then 
  134.             begin
  135.                 temp := ErrorWindow(0,c1Hndl^,_ToolErr);
  136.                 DisposeHandle(theHndl);
  137.                 CloseGS(CloseBlock);
  138.                 exit(putfileIntoWindow);
  139.             end;
  140.  
  141.         CloseGS (CloseBlock);
  142.         if _ToolErr <> 0 then 
  143.             begin
  144.                 temp := ErrorWindow(0,c1Hndl^,_ToolErr);
  145.                 DisposeHandle(theHndl);
  146.                 exit(putfileIntoWindow);
  147.             end;
  148.  
  149.         
  150.         TESetText ( 
  151.             teDataIsTextBlock+refIsHandle*8,                { Text Descriptor }
  152.             TETextRef(theHndl),                                 { Text Ref }
  153.             0,                                                  { Text Length }
  154.             0,                                                  { Style Descriptor }
  155.             TEStyleRef(0),                                    { Style Ref }
  156.             TERecordHndl(GetCtlHandleFromID(
  157.                             FrontWindow,MainWindowID))        { Control Handle }
  158.             );
  159.                     
  160.         DisposeHandle(theHndl);
  161.         
  162.     end;
  163.  
  164. {**********************************************************************}
  165. {
  166. { putWindowIntoFile
  167. {
  168. { This routine opens the specified file, writes the window contents.
  169. {
  170. {**********************************************************************}
  171. procedure    putWindowIntoFile (c1Hndl : Handle);
  172.     var
  173.         destroyBlock : NameRecGS;
  174.         createBlock  : CreateRecGS;
  175.         openBlock      : OpenRecGS;
  176.         closeBlock      : RefNumRecGS;
  177.         writeBlock      : IORecGS;
  178.         theHndl      : Handle;
  179.         temp, err     : Integer;
  180.         totalSize      : LongInt;
  181.         
  182.     begin
  183.         with DestroyBlock do begin
  184.             pCount := 1;
  185.             pathname := GSString255Ptr(c1Hndl^);
  186.         end;
  187.         
  188.         DestroyGS(DestroyBlock);
  189.         if _toolErr <> 0
  190.             then if _toolErr <> fileNotFound then
  191.                 begin
  192.                     temp := ErrorWindow(0,c1Hndl^, _toolErr);
  193.                     exit(putWindowIntoFile);
  194.                 end;
  195.         
  196.         with CreateBlock do Begin
  197.             pCount := 4;
  198.             pathname := GSString255Ptr(c1Hndl^);
  199.             access := $C3;
  200.             fileType := $04;
  201.             AuxType := 0;
  202.         end;
  203.         
  204.         CreateGS (CreateBlock);
  205.         if _ToolErr <> 0 then 
  206.             begin
  207.                 temp := ErrorWindow(0,c1Hndl^,_ToolErr);
  208.                 exit(PutWindowIntoFile);
  209.             end;
  210.             
  211.         with OpenBlock do begin
  212.             pCount := 15;
  213.             pathname := GSString255Ptr(c1Hndl^);
  214.             requestAccess := 0;
  215.             resourceNumber := 0;
  216.             optionList := NIL;
  217.         end;
  218.         
  219.         OpenGS (OpenBlock);
  220.         err := _toolErr;
  221.         if err <> 0 then 
  222.             begin
  223.                 DestroyGS(destroyBlock);
  224.                 temp := ErrorWindow(0,c1Hndl^,err);
  225.                 exit(PutWindowIntoFile);
  226.             end;
  227.  
  228.         with CloseBlock do begin
  229.             pCount := 1;
  230.             refNum := openBlock.refNum;
  231.         end;
  232.  
  233.         theHndl := NewHandle(1, userID, 0, NIL);
  234.  
  235.         if _toolErr = 0 then
  236.             totalSize := TEGetText (  
  237.                             teDataIsTextBlock+refIsHandle*8,                      { Text Descriptor }
  238.                             TETextRef(theHndl),                                     { Text Ref }
  239.                             0,                                                      { Text Length }
  240.                             0,                                                      { Style Descriptor }
  241.                             TEStyleRef(0),                                        { Style Ref }
  242.                             TERecordHndl(
  243.                                 GetCtlHandleFromID(FrontWindow,MainWindowID)
  244.                                         ))                                       { Control Handle }
  245.         else theHndl := NIL;                                    
  246.         
  247.         err := _toolErr;
  248.         if err <> 0 then 
  249.             begin
  250.                 if theHndl <> NIL then DisposeHandle(theHndl);
  251.                 CloseGS(CloseBlock);
  252.                 DestroyGS(destroyBlock);
  253.                 temp := ErrorWindow(0,c1Hndl^,err);
  254.                 exit(putWindowIntoFile);
  255.             end;
  256.             
  257.         HLock (theHndl);
  258.         with WriteBlock do begin
  259.             pCount := 4;
  260.             refNum := OpenBlock.refNum;
  261.             dataBuffer := theHndl^;
  262.             requestCount := totalSize;
  263.         end;
  264.         WriteGS (writeBlock);
  265.         err := _toolErr;
  266.         DisposeHandle(theHndl);
  267.         if err <> 0 then 
  268.             begin
  269.                 CloseGS(closeBlock);
  270.                 DestroyGS(destroyBlock);
  271.                 temp := ErrorWindow(0,c1Hndl^,err);
  272.                 exit(PutWindowIntoFile);
  273.             end;
  274.  
  275.         CloseGS (CloseBlock);
  276.         err := _toolErr;
  277.         if err <> 0 then
  278.             begin
  279.                 DestroyGS(destroyBlock);
  280.                 temp := ErrorWindow(0,c1Hndl^,err);
  281.             end;
  282.     end;
  283.  
  284.  
  285. {**********************************************************************}
  286. {
  287. { placeAndShowWindow
  288. {
  289. { This routine moves the specified window based on stagger count
  290. { and shows it.
  291. {
  292. {**********************************************************************}
  293. procedure placeAndShowWindow (theWindow : GrafPortPtr);
  294.  
  295.     begin
  296.         MoveWindow (8+8*staggerCount,28+8*staggerCount,theWindow);
  297.         staggerCount := staggerCount+1;
  298.         ShowWindow(theWindow);
  299.         SelectWindow(theWindow);                
  300.     end;
  301.     
  302. {****************************************************************************}
  303. {
  304. { doOpenWindow
  305. {
  306. { This routine either asks the user what file to open and opens it.
  307. {
  308. {****************************************************************************}
  309. procedure doOpenWindow;
  310.     var
  311.         myReply    : SFReplyRec2;
  312.         wptr    : GrafPortPtr;
  313.         myPrompt: string;
  314.         
  315.     begin
  316.         myPrompt := 'Pick a file, any file.';
  317.         
  318.         SFAllCaps (true);
  319.         
  320.         myReply.nameRefDesc := refIsNewHandle;
  321.         myReply.pathRefDesc := refIsNewHandle;
  322.         SFGetFile2 (10,35,
  323.                     RefIsPointer,
  324.                     Ref(@myPrompt),
  325.                     NIL,        {filter proc}
  326.                     NIL,        {type list}
  327.                     myReply );
  328.         
  329.                     
  330.         if myReply.good then
  331.             begin
  332.                 { Convert the C1Output string into a C1Input string for the resource mangager }
  333.                 C1OutputToC1Input(myReply.pathRef.RefIsHandle);
  334.                 
  335.                 C1OutputToPString(myReply.nameRef.RefIsHandle);
  336.                 
  337.                 HLock(myReply.nameRef.RefIsHandle);
  338.                 
  339.                 wptr := NewWindow2    (StringPtr(myReply.nameRef.RefIsHandle^),            { Title reference}
  340.                                      longint(myReply.pathRef.RefIsHandle),        { Ref con }
  341.                                      @DrawThisWindow,                            { Draw routine }
  342.                                      NIL,                                        { DefProc pointer }
  343.                                      RefIsResource,                                { Param Table Descriptor }
  344.                                      ref(MainWindowID),                            { Param Table Reference }
  345.                                      rWindParam1);                                { Param Table Type }
  346.  
  347.                 { Get rid of this handle }
  348.                 DisposeHandle(myReply.nameRef.RefIsHandle);
  349.                 
  350.                 placeAndShowWindow(wptr);
  351.                 
  352.                 putFileIntoWindow(myReply.pathRef.RefIsHandle);
  353.             end;
  354.   
  355.     end;
  356.  
  357.  
  358. {****************************************************************************}
  359. {
  360. { doSaveAs
  361. {
  362. { This routine saves the file  in the place indicated by the user.
  363. {
  364. {****************************************************************************}
  365. procedure doSaveAs;
  366.     var
  367.         myPrompt : String;
  368.         myReply : SFReplyRec2;
  369.         origWinTitle : StringPtr;
  370.         newWinTitle : Handle;
  371.         temp : LongInt;
  372.                 
  373.     begin;
  374.         MyPrompt := 'Give it a name, any name.';
  375.         SFAllCaps (true);
  376.         
  377.         myReply.nameRefDesc := refIsNewHandle;
  378.         myReply.pathRefDesc := refIsNewHandle;
  379.  
  380.         { The current window title is the default name }
  381.         origWinTitle := smartGetWTitle(FrontWindow);
  382.                         
  383.         { Get a C1String in  a new handle }
  384.         newWinTitle := PStringToNewC1String(origWinTitle);
  385.         
  386.         HLock(newWinTitle);
  387.         
  388.         SFPutFile2 (180,35,
  389.                     refIsPointer,
  390.                     Ref(@MyPrompt),
  391.                     refIsPointer,
  392.                     Ref(newWinTitle^),
  393.                     MyReply );
  394.         
  395.         { Don't need this default name any more }
  396.         DisposeHandle(newWinTitle);
  397.     
  398.         if myReply.good then
  399.             begin
  400.                 { Get rid of old path name handle in RefCon }
  401.                 DisposeHandle(Handle (GetWRefCon(FrontWindow)));
  402.  
  403.                 { Convert the C1Output string into a C1Input string for the resource mangager }
  404.                 C1OutputToC1Input(myReply.pathRef.RefIsHandle);
  405.  
  406.                 { Save the path name in the refcon of the window }
  407.                 SetWRefCon(LongInt(myReply.pathRef.RefIsHandle),FrontWindow);
  408.                 
  409.                 { Convert this C1 Output string into a pString for the window manager }
  410.                 C1OutputToPString(myReply.nameRef.RefIsHandle);
  411.                 
  412.                 SetWTitle (String255Ptr(myReply.nameRef.RefIsHandle^)^,FrontWindow);
  413.                 DisposeHandle(myReply.nameRef.RefisHandle);
  414.                 
  415.                 { Write the file out to disk }
  416.                 putWindowIntoFile(myReply.pathRef.RefIsHandle);
  417.             end;
  418.   
  419.     end;
  420.     
  421.  
  422. {****************************************************************************}
  423. {
  424. { doSave
  425. {
  426. { This routine either saves the file (unless it is new then it does a save 
  427. { as).
  428. {
  429. {****************************************************************************}
  430. procedure doSave;
  431.     var
  432.         wptr : GrafPortPtr;
  433.         refCon : LongInt;
  434.         
  435.     begin
  436.     
  437.         wptr := FrontWindow;
  438.         refCon := GetWRefCon(wptr);
  439.         
  440.         if refCon = 0 then doSaveAs
  441.         else  putWindowIntoFile(handle(refCon));
  442.     end;
  443.     
  444.     
  445.  
  446.  
  447. {****************************************************************************}
  448. {
  449. { doNewWindow
  450. {
  451. { This routine opens a new untitled window.
  452. {
  453. {****************************************************************************}
  454. procedure    doNewWindow;
  455.     var
  456.         wptr : GrafPortPtr;
  457.         tempStr : Str255;
  458.     begin
  459.         tempStr := 'Untitled';
  460.         wptr := NewWindow2    (StringPtr(@tempStr),            { Title reference}
  461.                              0,                        { Ref con }
  462.                              @drawThisWindow,        { Draw routine }
  463.                              NIL,                    { DefProc pointer }
  464.                              RefIsResource,            { Param Table Descriptor }
  465.                              Ref(MainWindowID),        { Param Table Reference }
  466.                              rWindParam1);            { Param Table Type }
  467.         
  468.         placeAndShowWindow(wptr);
  469.  
  470.     end;
  471. {*****************************************************************************}
  472. {
  473. { setUpWindows
  474. {
  475. { Opens the default window.
  476. {
  477. {*****************************************************************************}
  478. procedure setUpWindows;
  479.     begin   {of SetUpWindows}
  480.         doNewWindow;
  481.     end;    {of SetUpWindows}
  482.     
  483.